<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wsg:hbpz:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wsg:hbpz:edit']"
        >修改</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wsg:hbpz:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wsg:hbpz:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="hbpzList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="红包配置名称" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="dataType" :formatter="hbpzTypeFormat"/>
      <el-table-column label="总金额" align="center" prop="totalPrice" />
      <el-table-column label="总数量" align="center" prop="totalNum" />
      <el-table-column label="已发放金额" align="center" prop="providePrice" />
      <el-table-column label="已发放数量" align="center" prop="provideNum" />
      <el-table-column label="最低金额" align="center" prop="minPrice" />
      <el-table-column label="开始时间" align="center" prop="startDate" />
      <el-table-column label="结束时间" align="center" prop="endDate" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wsg:hbpz:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wsg:hbpz:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改万寿宫-红包活动配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="form" label-width="120px">
        <el-col :span="12">
          <el-form-item label="红包配置名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入红包配置名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型">
            <el-select v-model="form.dataType" placeholder="请选择类型" clearable size="small" :disabled="form.dataTypeDisabled">
              <el-option
                v-for="dict in hbpzTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总金额" prop="totalPrice">
            <el-input v-model="form.totalPrice" placeholder="请输入总金额" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总数量" prop="totalNum">
            <el-input v-model="form.totalNum" placeholder="请输入总数量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已发放金额" prop="providePrice">
            <el-input v-model="form.providePrice" placeholder="请输入已发放金额" :disabled="true"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已发放数量" prop="provideNum">
            <el-input v-model="form.provideNum" placeholder="请输入已发放数量" :disabled="true"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最低金额" prop="minPrice">
            <el-input v-model="form.minPrice" placeholder="请输入最低金额" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="时间">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-select v-model="form.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHbpz, getHbpz, delHbpz, addHbpz, updateHbpz, exportHbpz } from "@/api/wsg/hbpz";

export default {
  name: "Hbpz",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 万寿宫-红包活动配置表格数据
      hbpzList: [],
      // 红包配置状态
      statusOptions:[],
      // 红包配置类型
      hbpzTypeOptions:[],
      // 弹出层标题
      title: "",
      // 日期范围
      dateRange: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: undefined,
        wxClientId: undefined,
        dataType: undefined,
        totalPrice: undefined,
        totalNum: undefined,
        providePrice: undefined,
        provideNum: undefined,
        minPrice: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        dataType: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "总金额不能为空", trigger: "blur"},
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: "请输入正确的总金额",
            trigger: "blur"
          }
        ],
        totalNum: [
          { required: true, message: "总数量不能为空", trigger: "blur"},
          {
            pattern: /(^[1-9]\d*$)/,
            message: "请输入正确的总数量",
            trigger: "blur"
          }
        ],
        minPrice: [
          { required: true, message: "最低金额不能为空", trigger: "blur"},
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: "请输入正确的最低金额",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    // 获取红包配置状态
    this.getDicts("wsg_hbpz_status").then(response => {
      this.statusOptions = response.data;
    });
    // 获取红包配置类型
    this.getDicts("wsg_hbpz_type").then(response => {
      this.hbpzTypeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询万寿宫-红包活动配置列表 */
    getList() {
      this.loading = true;
      listHbpz(this.queryParams).then(response => {
        this.hbpzList = response.rows;
        console.log(this.hbpzList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        status: "0",
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        wxClientId: undefined,
        dataType: undefined,
        totalPrice: undefined,
        totalNum: undefined,
        providePrice: undefined,
        provideNum: undefined,
        minPrice: undefined
      };
      this.resetForm("form");
      this.dateRange = [];
    },
    // 红包配置状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 红包配置状态字典翻译
    hbpzTypeFormat(row, column) {
      return this.selectDictLabel(this.hbpzTypeOptions, row.dataType);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.dataTypeDisabled = false;
      console.log( this.form)
      this.open = true;
      this.title = "添加万寿宫-红包活动配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getHbpz(id).then(response => {
        this.form = response.data;
        this.form.dataTypeDisabled = true;
        console.log(this.form);
        this.dateRange[0] = this.form.startDate;
        this.dateRange[1] = this.form.endDate;
        this.open = true;
        this.title = "修改万寿宫-红包活动配置";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      console.log(this.dateRange);
      var status = this.form.status;
      if (status == '' || status == null || status == undefined) {
        this.msgError("请选择状态。");
        return;
      }
      var dataType = this.form.dataType;
      if (dataType == '' || dataType == null || dataType == undefined) {
        this.msgError("请选择类型。");
        return;
      }
      if (parseFloat(this.form.totalPrice) <= parseFloat(this.form.minPrice)) {
        this.msgError("最小金额不能大于等于总金额。");
        return;
      }
      var dateRange = this.dateRange;
      if (dateRange == null) {
        this.msgError("请选择时间。");
        return;
      }
      this.form.startDate = dateRange[0];
      this.form.endDate = dateRange[1];
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateHbpz(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addHbpz(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除万寿宫-红包活动配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHbpz(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有万寿宫-红包活动配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportHbpz(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style scoped>
  .form{
    height: 320px;
  }
</style>
